package java学习.游卡;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * **************
 * 项目名称: 蓝桥杯-Java学习 <br/>
 * 文件名称:  <br/>
 * 文件描述: 这里添加您的类文件描述，说明当前文件要包含的功能。 <br/>
 * 文件创建：刘世锦Lenovo <br/>
 * 创建时间: 2022/11/3 <br/>
 *
 * @version v1.0 <br/>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br/>
 * *************
 */

public class 字符串 {
    public static void main(String[] args) {
        String str1 = "abcyokagames",str2 = "12345";
        String s = "deeedbbcccbdaa";
//        System.out.println(alternativeMerge2(str1,str2));
        System.out.println(removeDuplicates(s,3));
    }


    public ArrayList<TreeNode> findCommonAncestors2 (TreeNode root, TreeNode p, TreeNode q) {
        // write code here
        ArrayList list =  new ArrayList<TreeNode>();
        HashMap<TreeNode, TreeNode> map = new HashMap<>();
        if(p==null||q==null){
            return list;
        }
        while (root!=null){
            if (root.left==p){
                map.put(root,p);
            }
            if (root.right==p){
                map.put(root,p);
            }
            if (root.left==q){
                map.put(root,q);
            }
            if (root.right==q){
                map.put(root,q);
            }
            
            root = root.left;
        }
        return list;
    }
    public static String removeDuplicates (String s, int k) {
        String res = func(s, k);
        while (res.length()>k&&!s.equals(res)){
            res = func(res,k);
        }
        return res;
    }

    public static String func (String s, int k) {
//        0 1 2 3 4 5 6 4 8 9
//          i
//        d e e e d b b c c c b d a a
//                j   ddada
        StringBuilder res = new StringBuilder();
        int n = s.length();
        if (n<k){
            return s;
        }
        int count = k;
        for (int i = 0,j=0; i < s.length();  ) {
            while (count>1&&s.charAt(j)==s.charAt(i)){
                j++;count--;
            }
            count = k;
            if (j-i<count-1){
                res.append(s.charAt(i));
                i++;j=i;
            }else if (j-i==2){
                if (j<=s.length()-1){
                    String substring = s.substring(j + 1);
                     res.append(substring).toString();
                     break;
                }
            }

        }
        return  res.toString();
    }

    public static String alternativeMerge2 (String str1, String str2) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0,j=0; i < str1.length();  ) {
                builder.append(str1.charAt(i++));
                if (j==str2.length()){
                    j = 0;
                    builder.append(str2.charAt(j++));
                }else{
                    builder.append(str2.charAt(j++));
                }
        }
        System.out.println(builder);
        return  builder.toString();
    }
}
 class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}